<?php
/**
 * Created by PhpStorm.
 * User: huqb
 * Date: 15/5/13
 * Time: 下午3:53
 */
    class Wechat extends CI_Controller{
        private $token;

        public function __construct() {
            parent::__construct();
            $this->load->library('validate');
            $this->load->library('receive');
        }

        public function index(){
            if (!isset($_GET['echostr'])) {
                $this->responseMsg();
            }else{
                $this->validate->valid();
            }
        }

        //响应消息
        public function responseMsg(){

            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
            if (!empty($postStr)) {
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $RX_TYPE = trim($postObj->MsgType);

                //消息类型分离
                switch ($RX_TYPE) {
                    case "event":
                        $result = $this->receive->receiveEvent($postObj);
                        break;
                    case "text":
                        $result = $this->receive->receiveText($postObj);
                        break;
                    case "image":
                        $result = $this->receive->receiveImage($postObj);
                        break;
                    case "location":
                        $result = $this->receive->receiveLocation($postObj);
                        break;
                    case "voice":
                        $result = $this->receive->receiveVoice($postObj);
                        break;
                    case "video":
                        $result = $this->receive->receiveVideo($postObj);
                        break;
                    case "link":
                        $result = $this->receive->receiveLink($postObj);
                        break;
                    default:
                        $result = "unknown msg type: " . $RX_TYPE;
                        break;
                }
                echo $result;
            } else {
                echo "";
                exit;
            }
        }
    }